package com.frxuexi.demo.diff


import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.RequestOptions
import com.frxuexi.demo.R


/**
 * 介绍：普普通的adapter，
 * 但是 唯一亮点~
 * public void onBindViewHolder(DiffVH holder, int position, List<Object> payloads)
 * 重写这个方法
 * cc--
 * cc 96.10.11
</Object> */
public class DiffAdapter(private val mContext: Context, mDatas: List<TestBean>?) :
    RecyclerView.Adapter<DiffAdapter.DiffVH>() {
    private var mDatas: List<TestBean>?
    private val mInflater: LayoutInflater

    init {
        this.mDatas = mDatas
        mInflater = LayoutInflater.from(mContext)
    }

    fun setDatas(mDatas: List<TestBean>?) {
        this.mDatas = mDatas
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DiffVH {
        return DiffVH(mInflater.inflate(R.layout.item_diff, parent, false))
    }

    override fun onBindViewHolder(holder: DiffVH, position: Int) {
        val bean: TestBean = mDatas!![position]
        holder.mTvName.setText(bean.name)
        holder.mTvContent.setText(bean.content)
        displayCircleImage(mContext, bean.pic, holder.mImgUrl, 0)
        holder.mTvLookNumber.setText(StringBuffer("浏览量：").append(bean.lookNumber))
        holder.mTvCommentNumber.setText(StringBuffer("评论量：").append(bean.commentNumber))
    }

    override fun onBindViewHolder(holder: DiffVH, position: Int, payloads: List<Any>) {
        if (payloads.isEmpty()) {
            onBindViewHolder(holder, position)
        } else {
            //文艺青年中的文青
            val payload = payloads[0] as Bundle //取出我们在getChangePayload（）方法返回的bundle
            val bean: TestBean = mDatas!![position] //取出新数据源，（可以不用）
            for (key in payload.keySet()) {
                when (key) {
                    "KEY_DESC" ->                         //这里可以用payload里的数据，不过data也是新的 也可以用
                        holder.mTvContent.setText(bean.content)

                    "KEY_PIC" -> displayCircleImage(
                        mContext,
                        payload.getInt(key),
                        holder.mImgUrl,
                        0
                    )

                    "KEY_LOOK_NUMBER" -> holder.mTvLookNumber.text =
                        StringBuffer("浏览量：").append(payload.getInt(key))

                    "KEY_COMMENT_NUMBER" -> holder.mTvCommentNumber.text =
                        StringBuffer("评论量：").append(payload.getInt(key))

                    else -> {}
                }
            }
        }
    }

    override fun getItemCount(): Int {
        return if (mDatas != null) mDatas!!.size else 0
    }

    inner class DiffVH(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var mImgUrl: ImageView
        var mTvName: TextView
        var mTvContent: TextView
        var mTvLookNumber: TextView
        var mTvCommentNumber: TextView

        init {
            mImgUrl = itemView.findViewById<ImageView>(R.id.img_url)
            mTvName = itemView.findViewById<TextView>(R.id.txt_name)
            mTvContent = itemView.findViewById<TextView>(R.id.txt_content)
            mTvLookNumber = itemView.findViewById<TextView>(R.id.tv_look_number)
            mTvCommentNumber = itemView.findViewById<TextView>(R.id.tv_comment_number)
        }
    }

    companion object {
        private const val TAG = "zxt"
        fun displayCircleImage(
            context: Context?,
            url: Int,
            imageView: ImageView?,
            placeHolderId: Int
        ) {
            val options = RequestOptions().circleCrop().placeholder(placeHolderId)
            Glide.with(context!!).load(url).transition(DrawableTransitionOptions.withCrossFade())
                .apply(options).into(
                    imageView!!
                )
        }
    }
}

